package com.poly.common.core.web.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Query基类
 *
 * @author Poly
 */
public class BaseQuery implements Serializable {
    private static final long serialVersionUID = 1L;


    /**
     * 页码
     */
    private Integer pageNum = 1;

    /**
     * 页数
     */
    private Integer pageSize = 20;

    /**
     * 请求参数
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Map<String, Object> params;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Integer getOffset() {
        return (Math.max(pageNum, 1) - 1) * pageSize;
    }

    public IPage getPage(){
        return new Page<>(getPageNum(), getPageSize());
    }
}
